package day18;
//通过synchronized模拟死锁
public class Demo4 {
    public static void main(String[] args) {
        Demo4 d1=new Demo4();
        Demo4 d2=new Demo4();
        //线程1：先添加d1的锁，再添加d2的锁
        new Thread(()->{
            synchronized (d1){
                System.out.println("线程1获取了第d1锁,等待获取d2的锁");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (d2){
                    System.out.println("线程1获取了第d1锁，也获取了d2" );
                }
                System.out.println("线程1释放了d2的锁");
            }
            System.out.println("线程1释放了d1的锁");
        }).start();
        new Thread(()->{
            synchronized (d2){
                System.out.println("线程2获取了第d2锁,等待获取d1的锁");
                synchronized (d1){
                    System.out.println("线程2获取了第d1锁，也获取了d2" );
                }
                System.out.println("线程2释放了d1的锁");
            }
            System.out.println("线程2释放了d2的锁");
        }).start();
    }
}
